package org.basic.mail.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.basic.mail.domain.bo.SysMailAccountBo;
import org.basic.mail.domain.vo.SysMailAccountVo;
import org.basic.mail.domain.vo.SysSimpleMailAccountVo;
import org.basic.mail.service.ISysMailAccountService;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.basic.common.idempotent.annotation.RepeatSubmit;
import org.basic.common.log.annotation.Log;
import org.basic.common.web.core.BaseController;
import org.basic.common.mybatis.core.page.PageQuery;
import org.basic.common.core.domain.R;
import org.basic.common.core.validate.AddGroup;
import org.basic.common.core.validate.EditGroup;
import org.basic.common.log.enums.BusinessType;
import org.basic.common.excel.utils.ExcelUtil;

import org.basic.common.mybatis.core.page.TableDataInfo;

/**
 * 邮箱账号
 *
 * @author Shao Hua
 * @date 2023-07-27
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/mailAccount")
public class SysMailAccountController extends BaseController {

    private final ISysMailAccountService sysMailAccountService;

    /**
     * 查询邮箱账号列表
     */
    @SaCheckPermission("system:mailAccount:list")
    @GetMapping("/list")
    public TableDataInfo<SysMailAccountVo> list(SysMailAccountBo bo, PageQuery pageQuery) {
        return sysMailAccountService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出邮箱账号列表
     */
    @SaCheckPermission("system:mailAccount:export")
    @Log(title = "邮箱账号", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(SysMailAccountBo bo, HttpServletResponse response) {
        List<SysMailAccountVo> list = sysMailAccountService.queryList(bo);
        ExcelUtil.exportExcel(list, "邮箱账号", SysMailAccountVo.class, response);
    }

    /**
     * 获取邮箱账号详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("system:mailAccount:query")
    @GetMapping("/{id}")
    public R<SysMailAccountVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(sysMailAccountService.queryById(id));
    }

    /**
     * 新增邮箱账号
     */
    @SaCheckPermission("system:mailAccount:add")
    @Log(title = "邮箱账号", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody SysMailAccountBo bo) {
        return toAjax(sysMailAccountService.insertByBo(bo));
    }

    /**
     * 修改邮箱账号
     */
    @SaCheckPermission("system:mailAccount:edit")
    @Log(title = "邮箱账号", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody SysMailAccountBo bo) {
        return toAjax(sysMailAccountService.updateByBo(bo));
    }

    /**
     * 删除邮箱账号
     *
     * @param ids 主键串
     */
    @SaCheckPermission("system:mailAccount:remove")
    @Log(title = "邮箱账号", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(sysMailAccountService.deleteWithValidByIds(List.of(ids), true));
    }

    /**
     * 获取邮箱数组信息
     * @return
     */
    @GetMapping("/list-all-simple")
    public R<List<SysSimpleMailAccountVo>> getSimpleMailAccountList() {
        return R.ok(sysMailAccountService.getSimpleMailAccountList());
    }
}
